package com.cskaoyan.service;

import com.cskaoyan.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    AccountDao accountDao;

    @Override
    public void transfer(int fromId, int destId, double money) throws SQLException {
        //TransactionUtils.startTransaction();

        //可以把中间的这部分做一个try-catch，如果有异常可以执行rollback
        double fromMoney = accountDao.queryMoney(fromId) - money;
        double destMoney = accountDao.queryMoney(destId) + money;

        accountDao.update(fromId,fromMoney);
        //制造异常
        //int i = 1/0;
        accountDao.update(destId,destMoney);

        //TransactionUtils.commitTransaction();
    }

    @Override
    public void save(int id, double money) throws SQLException {
        //TransactionUtils.startTransaction();
        accountDao.update(id, money);
        //TransactionUtils.commitTransaction();
    }
}
